package com.ants.modules.friendLinks.controller;

import com.ants.common.annotation.AutoLog;
import com.ants.common.system.query.QueryGenerator;
import com.ants.common.system.result.Result;
import com.ants.modules.friendLinks.entity.FriendLinks;
import com.ants.modules.friendLinks.service.FriendLinksService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 * Author Chen
 * Date   2021/9/7 18:44
 */

@Slf4j
@Api(tags = "友链")
@RestController
@RequestMapping("/friendLinks")
public class FriendLinksController {

    @Autowired
    FriendLinksService friendLinksService;

    @AutoLog(value = "友链-列表")
    @ApiOperation(value = "友链-列表", notes = "友链-列表")
    @GetMapping("/list")
    public Result<?> queryPageList(FriendLinks friendLinks,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FriendLinks> queryWrapper = QueryGenerator.initQueryWrapper(friendLinks, req.getParameterMap());
        Page<FriendLinks> page = new Page<FriendLinks>(pageNo, pageSize);
        IPage<FriendLinks> pageList = friendLinksService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "友链-列表")
    @ApiOperation(value = "友链-列表", notes = "友链-列表")
    @GetMapping("/initFriendLinks")
    public Result<?> initFriendLinks(FriendLinks friendLinks,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {
        QueryWrapper<FriendLinks> queryWrapper = QueryGenerator.initQueryWrapper(friendLinks, req.getParameterMap());
        queryWrapper.select(FriendLinks.class, info -> !info.getColumn().equals("email"));
        List<FriendLinks> list = friendLinksService.list(queryWrapper);
        return Result.ok(list);
    }

    @Autowired
    AmqpTemplate rabbitTemplate;

    /**
     * 添加
     *
     * @param friendLinks
     * @return
     */
    @AutoLog(value = "友链-添加")
    @ApiOperation(value = "友链-添加", notes = "友链-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FriendLinks friendLinks) {
        friendLinks.setLink(friendLinks.getLink());
        friendLinks.setWebLogo(friendLinks.getWebLogo());
        boolean save = friendLinksService.save(friendLinks);

        Map<String, String> map = new HashMap<>();
        map.put("name", friendLinks.getName());
        map.put("link", friendLinks.getLink());
        map.put("webDescribe", friendLinks.getWebDescribe());
        // 提交申请之后给管理员发邮件通知
        rabbitTemplate.convertAndSend("friendLinksApplyQueue", map);
        if (save) {
            return Result.ok("提交成功，等待审核！");
        }
        return Result.error("提交失败！");
    }


    /**
     * 编辑
     *
     * @param friendLinks
     * @return
     */
    @AutoLog(value = "友链-编辑")
    @ApiOperation(value = "友链-编辑", notes = "友链-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FriendLinks friendLinks) {
        boolean b = friendLinksService.updateById(friendLinks);


        Map<String, String> map = new HashMap<>();
        map.put("name", friendLinks.getName());
        map.put("link", friendLinks.getLink());
        map.put("webDescribe", friendLinks.getWebDescribe());
        map.put("start", "1".equals(friendLinks.getState()) ? "审核通过" : "审核不通过");
        map.put("feedback", friendLinks.getFeedback());
        map.put("email", friendLinks.getEmail());
        rabbitTemplate.convertAndSend("friendLinksApplyFeedbackQueue", map);
        if (b) {
            return Result.ok("操作成功!");
        }
        return Result.error("操作失败!");
    }

    /**
     * @param id
     * @return
     * @功能：删除
     */
    @AutoLog(value = "友链-删除")
    @ApiOperation(value = "友链-删除", notes = "友链-删除")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        boolean ok = friendLinksService.removeById(id);
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @AutoLog(value = "友链-批量删除")
    @ApiOperation(value = "友链-批量删除", notes = "友链-批量删除")
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        boolean ok = friendLinksService.removeByIds(Arrays.asList(ids.split(",")));
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }


}
